package com.shengdou.as.slopsapptest.Activity;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.bean.person;
import com.shengdou.as.slopsapptest.utils.CheckPhoneUtil;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.FileUtils;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;
import com.shengdou.as.slopsapptest.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 添加管理人员
 */
public class SetManagerActivity extends BaseActivity {
    private EditText mEditName, mEditPhone;
    private Map<String, String> params = new HashMap<>();
    private int id;
    private PopupWindow popup = null;
    private View v;
    int idlist;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.USERADD:
                    String result = msg.obj.toString();
                    LogUtil.e("result", result);
                    params.clear();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        ToastUtils.showToast(jsonObject.getString("status_text"));
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case Constants.USEREDIT:
                    String results = msg.obj.toString();
                    LogUtil.e("result", results);
                    params.clear();
                    try {
                        JSONObject jsonObject = new JSONObject(results);
                        ToastUtils.showToast(jsonObject.getString("status_text"));
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_manager);
        init();
    }

    private void init() {
        idlist = getIntent().getIntExtra("id", 0);
        v = findViewById(R.id.view);
        mEditName = (EditText) findViewById(R.id.edit_name);
        mEditPhone = (EditText) findViewById(R.id.edit_phone);
        if (idlist != 0) {
            mEditName.setText(getIntent().getStringExtra("name"));
            mEditPhone.setText(getIntent().getStringExtra("mobile"));
        }
    }


    public void orderClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.done:
                String name = mEditName.getText().toString().trim();
                String phone = mEditPhone.getText().toString().trim();
                if (!CheckPhoneUtil.isPhoneNumberValid(phone) || phone.length() != 11) {
                    ToastUtils.showToast(R.string.phone_format);
                    return;
                } else if (name == null || name.equals("")) {
                    ToastUtils.showToast(R.string.edit_name);
                    return;
                } else if (CheckPhoneUtil.isPhoneNumberValid(phone) && name != null) {
                    params.put("mobile", phone);
                    params.put("name", name);
                    params.put("factory_id", id + "");
                    if (idlist == 0) {
                        HttpUtils.post(mHandler, NetConstant.USERADD(), params, Constants.USERADD, SetManagerActivity.this);
                    } else {
                        params.put("uid", idlist + "");
                        HttpUtils.post(mHandler, NetConstant.USEREDIT(), params, Constants.USEREDIT, SetManagerActivity.this);
                    }
                }
                break;
        }
    }
}
